/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/threads
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AESemaphore.h
// Author:		Gianluca Belardelli
// Date:		13/03/2014
//
/////////////////////////////////////////////////////////////////////////
#ifndef _AESEMAPHORE_H_
#define _AESEMAPHORE_H_

class AE_COREDLLIMPEXP AESemaphore : public AEWaitableObject
{
// Members
public:
	enum AESemaphoreErrors
	{
		AESemaphore_FailedInitialize = 0,
		AESemaphore_Ok,
		AESemaphore_Timeout,
		AESemaphore_WaitError,
		AESemaphore_SetError
	};
private:
	AESEMAPHOREHANDLE	m_hSemaphore;

// Methods
private:
	AESemaphore( void );

public:
	/// Creates the semaphore. The current value of the semaphore is given in n. The
	/// maximum value of the semaphore is given in max. If only n is given, it must be greater than zero.
	/// If both n and max are given, max must be greater than zero, n must be greater than
	/// or equal to zero and less than or equal to max.
	AESemaphore( AEINT32 nValue, AEINT32 nMax );
		
	~AESemaphore( void );
	
	/// Increments the semaphore's value by one and
	/// thus signals the semaphore. Another thread
	/// waiting for the semaphore will be able
	/// to continue.
	AEINT32 Set( void );

	/// Waits for the semaphore to become signalled.
	/// To become signalled, a semaphore's value must
	/// be greater than zero. 
	/// Decrements the semaphore's value by one.
	AEINT32 WaitInfinite( void );

	AEINT32 Wait( AEINT32 uiMilliseconds );

#if defined( AE_PLATFORM_WIN32 )
	virtual AEHANDLE GetHandle( void ) { return m_hSemaphore; }
#else
	virtual AEHANDLE GetHandle( void ) { return (AEHANDLE)&m_hSemaphore; }
#endif
};

#endif // _AESEMAPHORE_H_
